package com.leo.oplayer.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.leo.oplayer.R;
import com.leo.oplayer.model.MonthStat;
import com.leo.oplayer.util.Constancts;
import com.leo.oplayer.util.PersonUtil;

public class MonthStatAdapter extends ArrayAdapter<MonthStat>{
	
	int resource;
	
	Integer max; //point max value
	
	Integer min; //point min value
	
	public MonthStatAdapter(Context _context, int _resource,List<MonthStat> _items) {
		super(_context, _resource, _items);
		this.resource = _resource;		
	}

	public MonthStatAdapter(Context _context, int _resource,List<MonthStat> _items,Integer min) {
		super(_context, _resource, _items);
		this.resource = _resource;
		
		if(_items != null && !_items.isEmpty()){
			this.max = _items.get(0).getPoint();
		}
		this.min = min;
		
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		LinearLayout todoView; 
		MonthStat stat = getItem(position); 
		String phoneId = stat.getPhoneId();
		Integer inCallNum = stat.getInCallNum();
		Integer outCallNum = stat.getOutCallNum();
		Integer point = stat.getPoint();
		Integer grade = this.getGrade(max, min, point);
		
		Log.i("t1111", "position:"+position+" /max:"+max+"/min:"+min+"/grade:"+grade);
		
		String personName = PersonUtil.findNameById(getContext(), phoneId);
		
		if (convertView == null) 
		{ 
			todoView = new LinearLayout(getContext()); 
			String inflater = Context.LAYOUT_INFLATER_SERVICE; 
			LayoutInflater vi; 
			vi = (LayoutInflater)getContext().getSystemService(inflater);
			vi.inflate(resource, todoView, true); 
		} 
		else { 
			todoView = (LinearLayout) convertView;
		} 
		TextView phone = (TextView)todoView.findViewById (R.id.phoneId); 
		TextView callIn = (TextView)todoView.findViewById(R.id.inCallNum); 
		TextView callOut = (TextView)todoView.findViewById(R.id.outCallNum);
		TextView msgIn = (TextView)todoView.findViewById(R.id.inMsgNum);
		TextView msgOut = (TextView)todoView.findViewById(R.id.outMsgNum);
		TextView name = (TextView)todoView.findViewById(R.id.phoneName);
		TextView v_grade = (TextView)todoView.findViewById(R.id.TextView01);
		
		phone.setText(phoneId);
		name.setText(personName);
		callIn.setText("("+inCallNum+")");
		callOut.setText("("+outCallNum.toString()+")"); 
		msgIn.setText("("+stat.getInMsgNum()+")"); 
		msgOut.setText("("+stat.getOutMsgNum()+")"); 
		v_grade.setText("grade:"+grade);
		
		
		return todoView; 
	}
	
	private Integer getGrade(Integer max,Integer min, Integer point){
		
		Log.d("RankView", "max:"+max+",min:"+min);
		int totalGrad = Constancts.SYSTEM_GRAD;
		
		if(max==null || min==null)
			return null;
		
		if((max-min)==0)
			return totalGrad;	
		
		//cur_grad = total_grad * (cur_point-min)/(max-min)
		int curGrad = totalGrad*(point-min)/(max-min);
		
		return curGrad+1;
		
	}
}
